function LifeToMaxRegister takes unit survivor , code fun returns nothing
	local trigger lifeToMaxTrigger = CreateTrigger()
	local integer triggerId = GetHandleId(lifeToMaxTrigger)
	//--
	call TriggerRegisterUnitStateEvent(lifeToMaxTrigger , survivor , UNIT_STATE_LIFE , GREATER_THAN_OR_EQUAL , GetUnitState(survivor , UNIT_STATE_MAX_LIFE))
	call SaveUnitHandle(udg_TriggerDataHashTable , triggerId , 2 , survivor)
	call SaveTriggerActionHandle(udg_TriggerDataHashTable , triggerId , 3 , TriggerAddAction(lifeToMaxTrigger , fun))
	//--
	set lifeToMaxTrigger = null
endfunction

function LifeBelowMinRegister takes unit survivor , code fun returns nothing
	local trigger lifeBelowMinTrigger = CreateTrigger()
	local integer triggerId = GetHandleId(lifeBelowMinTrigger)
	local real lifeNow = GetUnitState(survivor , UNIT_STATE_LIFE)
	//--
	call SaveReal(udg_UnitDataHashTable , GetHandleId(survivor) , StringHash("UNIT_TEMP_LIFE_MIN") , lifeNow)
	call TriggerRegisterUnitStateEvent(lifeBelowMinTrigger , survivor , UNIT_STATE_LIFE , LESS_THAN  , lifeNow - 1.0)
	call SaveUnitHandle(udg_TriggerDataHashTable , triggerId , 2 , survivor)
	call SaveTriggerActionHandle(udg_TriggerDataHashTable , triggerId , 3 , TriggerAddAction(lifeBelowMinTrigger , fun))
	call SaveTriggerHandle(udg_UnitDataHashTable , GetHandleId(survivor) , StringHash("LifeBelowMinTrigger") , lifeBelowMinTrigger)
	//--
	set lifeBelowMinTrigger = null
endfunction

function DestroyLifeBelowMinTrigger takes trigger lifeBelowMinTrigger returns nothing
	local integer triggerId = GetHandleId(lifeBelowMinTrigger)
	local unit survivor = LoadUnitHandle(udg_TriggerDataHashTable , triggerId , 2)
	local triggeraction action = LoadTriggerActionHandle(udg_TriggerDataHashTable , triggerId , 3)
	//--
	call TriggerRemoveAction(lifeBelowMinTrigger , action)
	call FlushChildHashtable(udg_TriggerDataHashTable , triggerId)
	call DestroyTrigger(lifeBelowMinTrigger)
	//--
	set lifeBelowMinTrigger = null
	set survivor = null
	set action = null
endfunction

function lifeBelowMinAction takes nothing returns nothing
	call DestroyLifeBelowMinTrigger(GetTriggeringTrigger())
	call LifeBelowMinRegister(GetTriggerUnit() , function lifeBelowMinAction)
endfunction

function lifeToMaxAction takes nothing returns nothing
	local trigger lifeToMaxTrigger = GetTriggeringTrigger()
	local integer triggerId = GetHandleId(lifeToMaxTrigger)
	local unit survivor = LoadUnitHandle(udg_TriggerDataHashTable , triggerId , 2)
	local triggeraction action = LoadTriggerActionHandle(udg_TriggerDataHashTable , triggerId , 3)
	local real maxHp = GetUnitState(survivor , UNIT_STATE_MAX_LIFE)
	local real minHp = LoadReal(udg_UnitDataHashTable , GetHandleId(survivor) , StringHash("UNIT_TEMP_LIFE_MIN"))
	local real hpRatio = (maxHp - minHp) / maxHp
	local real delta = 1 + 60.0 * hpRatio * hpRatio
	//--
	call SaveReal(udg_UnitDataHashTable , GetHandleId(survivor) , StringHash("UNIT_TEMP_LIFE_MIN") , GetUnitState(survivor , UNIT_STATE_MAX_LIFE) + delta - 1.0)
	call DestroyLifeBelowMinTrigger(LoadTriggerHandle(udg_UnitDataHashTable , GetHandleId(survivor) , StringHash("LifeBelowMinTrigger")))
	call LifeBelowMinRegister(survivor , function lifeBelowMinAction)
	call UnitAddMaxHp(survivor , delta)
	call TriggerRemoveAction(lifeToMaxTrigger , action)
	call FlushChildHashtable(udg_TriggerDataHashTable , triggerId)
	call DestroyTrigger(lifeToMaxTrigger)
	call LifeToMaxRegister(survivor , function lifeToMaxAction)
	//--
	set lifeToMaxTrigger = null
	set survivor = null
	set action = null
endfunction

function InitTrig_HPLimit takes nothing returns nothing
	local integer index = 0
	local trigger playerRecoverTrigger = null
	local unit survivor = null
    loop
    	if GetPlayerController(Player(index)) == MAP_CONTROL_USER  and GetPlayerSlotState(Player(index)) == PLAYER_SLOT_STATE_PLAYING then
			set survivor = udg_Survivors[index]
			call LifeToMaxRegister(survivor , function lifeToMaxAction)
			call LifeBelowMinRegister(survivor , function lifeBelowMinAction)
        endif
        set index = index + 1
        exitwhen index >= 10
    endloop
	set playerRecoverTrigger = null
	set survivor = null
endfunction

